package ui;

import engine.Vector2D;
import java.awt.Color;
import java.awt.Graphics;
import ui.DrawingScreen.Bounds;

public class GraphicCircle extends GraphicObject {

    protected Integer rayon;
    protected Color borderColor;

    public GraphicCircle(Vector2D position, Vector2D speed, Integer rayon, Color color, Color borderColor) {
        super(position, speed, color);

        this.rayon = rayon;
        this.borderColor = borderColor;
    }

    public Color getBorderColor() {
        return borderColor;
    }

    @Override
    public void draw(Graphics g) {
        Integer topLeftX, topLeftY, diameter;
        topLeftX = (int) (super.position.getX() - rayon);
        topLeftY = (int) (super.position.getY() - rayon);
        diameter = 2 * rayon;

        //Intérieur
        g.setColor(super.color);
        g.fillOval(topLeftX, topLeftY, diameter, diameter);

        //Bordure
        g.setColor(borderColor);
        g.drawOval(topLeftX, topLeftY, diameter, diameter);
    }

    @Override
    public boolean isInsideBounds(Bounds bounds) {
        //sort à gauche
        if (position.getX() - rayon < bounds.minX) {
            return false;
        }

        //sort à droite
        if (position.getX() + rayon > bounds.maxX) {
            return false;
        }

        //sort en haut
        if (position.getY() - rayon < bounds.minY) {
            return false;
        }

        //sort en bas
        if (position.getY() + rayon > bounds.maxY) {
            return false;
        }

        return true;
    }

    /**
     *
     * @param bounds limites à ne pas dépasser
     * @param maxMove déplacement maximal possible dans la direction de l'objet
     * @return
     */
    @Override
    public boolean isInsideBoundsNextStep(Bounds bounds, Vector2D maxMove) {

        boolean isInside = true;

        //Directions gauche/droite
        Double moveLeftRight;
        if (speed.getX() >= 0.) {//à droite
            moveLeftRight = bounds.maxX - position.getX() - rayon;
            if (moveLeftRight - speed.getX() < 0.) {
                super.speed.setX(-super.speed.getX());
                isInside = false;
            }
        } else {//à gauche
            moveLeftRight = bounds.minX - position.getX() + rayon;
            if (moveLeftRight - speed.getX() > 0.) {
                super.speed.setX(-super.speed.getX());
                isInside = false;
            }
        }

        //Directions haut/bas
        Double moveTopBotton;
        if (speed.getY() >= 0.) {//en bas
            moveTopBotton = bounds.maxY - position.getY() - rayon;
            if (moveTopBotton - speed.getY() < 0.) {
                super.speed.setY(-super.speed.getY());
                isInside = false;
            }
        } else {//en haut
            moveTopBotton = bounds.minY - position.getY() + rayon;
            if (moveTopBotton - speed.getY() > 0.) {
                super.speed.setY(-super.speed.getY());
                isInside = false;
            }
        }

        //On effectue le plus petit des déplacements verticaux ou horizontaux
        if (Math.abs(moveLeftRight) <= Math.abs(moveTopBotton)) {
            maxMove.setX(moveLeftRight);
            maxMove.setY(0.);
        } else {
            maxMove.setX(0.);
            maxMove.setY(moveTopBotton);
        }

        return isInside;
    }
}
